package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.example.exception.AppException;
import org.example.exception.AppExceptionCodeMsg;
import org.example.pojo.Register;
import org.example.query.RegisterQuery;
import org.example.service.IRegisterService;
import org.example.vo.ResultBean;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author peak
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/register")
public class RegisterController extends BaseController<Register, RegisterQuery> {
    @Autowired
    private IRegisterService registerService;
    @ApiOperation("修改挂号状态")
    @PostMapping("/change/state")
    public ResultBean changeState(@RequestBody Register register) {
        //参数检验
        //挂号的时候，什么也不填就直接添加成功了
        if(register==null|| register.getId()==null|| register.getVisitState()==null) {
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        UpdateWrapper<Register> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("visit_state", register.getVisitState())
                .eq("id", register.getId());
        boolean result = registerService.update(updateWrapper);
        return result? ResultBean.SUCCESS(): ResultBean.ERROR("修改挂号状态失败");

    }

    @PostMapping("/save")      //  请求方式 + 请求路径   post  /business
    public ResultBean save(@RequestBody Register register) throws Exception {
        //判断参数
        //先是个人的基本信息
        System.out.println(register.toString());
        if(register.getCardNumber()==null || register.getCardNumber().trim().length()==0
        || StringUtils.isNotEmpty(register.getRealName())
        || StringUtils.isNotEmpty(register.getHomeAddress())) {
            System.out.println("---------------");
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        if(register.getDepartment()==null || register.getEmployee()==null || register.getSettleCategory()==null) {
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        //开始添加或者是修改
        Boolean result = registerService.saveOrUpdate(register);
        if(result) {
            throw new AppException(AppExceptionCodeMsg.SAVE_FAIL);
        }
        return ResultBean.SUCCESS();
    }
    @PostMapping("/case/{caseNumber}")
    public ResultBean queryByCaseNumber(@PathVariable("caseNumber") String caseNumber) {
        QueryWrapper<Register> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("case_number", caseNumber);
        Register register = registerService.getOne(queryWrapper);
        return register==null? ResultBean.ERROR("患者不存在"): ResultBean.SUCCESS(register);
    }
    @PostMapping("/save1")      //  请求方式 + 请求路径   post  /business
    public ResultBean save1(@RequestBody Register register) throws Exception {
        if(register==null) {
         throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        boolean saved = registerService.saveOrUpdate(register);
        if(saved) {
            return ResultBean.SUCCESS();
        }
        return ResultBean.ERROR("添加挂号信息失败");
    }
}
